<?php
/**
 * 新华扬CRM用户管理控制器
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/1
 * Time: 11:37
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\Token;
use \app\api\service\Enterprise as EnterpriseService;
use \app\api\service\User as UserService;
use app\api\service\WorkAI;
use app\api\validate\CardBaseParameter;
use app\api\validate\CardExtendParameter;
use app\api\validate\CardProductParameter;
use app\api\validate\LabelParameter;
use app\api\validate\UserParameter;
use app\common\lib\Redis;
use app\lib\exception\UserException;
use think\Cache;
use think\Config;
use think\Hook;

class User extends BaseController
{
    /**
     * 请求用户详情
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws UserException
     */
    public function getUserDetails(){
        $uid = Token::getCurrentUid();
        $service = new \app\api\service\Card();
        $info =  $service->getUserInfoById($uid);
        if (empty($info)){
            throw new UserException([]);
        }
        $info->hidden(['session_key','unionid']);
        return $info;
    }
    /**
     * 获取名片码
     * @return \think\response\Json
     */
    public function getQrCode(){
        $w_id = Token::getCurrentUid();
        $card = new \app\api\service\Card();
        $url = $card->getQRCode($w_id);
        if (null != $url){
            $info['error_code'] = 0;
            $info['url'] = config('setting.url_prefix').$url;
            $data['code'] = 200;
            $data['content'] = $info;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 检查用户是否用户是否授权
     * @return string
     */
    public function checkAuthPhone($w_id){
        $id = explode('_',$w_id);
        $uid = Token::getCurrentUid();
        $user =  new UserService();
        $phone =  $user->checkAuthPhone($id[1],$uid);
        return json_encode($phone);
    }

    /**
     * 获取新华扬CRM用户信息
     * @return \think\response\Json
     */
    public function getUserInfo(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getUserInfo($wu_id);
        if (!empty($result)){
            $data['error_code'] = 200;
            $data['content'] = $result;
        }else{
            $data['error_code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 更新用户头像信息
     * @param $img
     * @return \think\response\Json
     */
    public function updateImg(){
        $wu_id = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new EnterpriseService();
        $dataArray = input('post.');
        $result = $service->updateImg($dataArray,$wu_id);
        if (!empty($result)){
            //清空缓存
            $tag = sprintf(config('cache.card_base'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            //清掉列表缓存
            $cache = Cache::connect(Config::get('cache.redis'));
            $cache->handler()->select(2);
            $cache->handler()->setOption($cache->handler()::OPT_SCAN,$cache->handler()::SCAN_RETRY);
            $iterator = null;
            while ($keys =  $cache->handler()->scan($iterator, 'card:list:t_id:'.$t_id.':*')) {
                foreach ($keys as $key) {
                    $cache->handler()->del($key);
                }
            }
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取新华扬CRM名片基本信息
     * @return \think\response\Json
     */
    public function getBaseInfo(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result = $service->getBaseInfo($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取新华扬CRM名片基本信息
     * @return \think\response\Json
     */
    public function getCardBaseInfo(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getCardBase($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 新华扬基本信息
     * @return \think\response\Json
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function updateCardBaseInfo(){
        (new CardBaseParameter())->goCheck();
        $wu_id = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('t_id');
        $dataArray = input('post.');
        $service = new EnterpriseService();
        $result = $service->updateCardBase($dataArray,$wu_id);
        if ($result){
            //清空缓存
            $tag = sprintf(config('cache.card_base'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            //清掉列表缓存
            $cache = Cache::connect(Config::get('cache.redis'));
            $cache->handler()->select(2);
            $cache->handler()->setOption($cache->handler()::OPT_SCAN,$cache->handler()::SCAN_RETRY);
            $iterator = null;
            while ($keys =  $cache->handler()->scan($iterator, 'card:list:t_id:'.$t_id.':*')) {
                foreach ($keys as $key) {
                    $cache->handler()->del($key);
                }
            }
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     * 新华扬CRM电话修改
     * @return \think\response\Json
     */
    public function updatePhone(){
        (new CardBaseParameter())->goCheck();
        $wu_id = Token::getCurrentUid();
        $dataArray = input('post.');
        $service = new EnterpriseService();
        $result =  $service->updatePhone($dataArray,$wu_id);
        if (!empty($result)){
            //清空缓存
            $tag = sprintf(config('cache.card_base'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     * 新华扬CRM获取推荐商品
     * @return \think\response\Json
     */
    public function getGoods(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getGoods($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     * 新华扬CRM添加推荐商品
     * @return \think\response\Json
     */
    public function addGoods(){
       (new CardProductParameter())->goCheck();
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $dataArray = input('post.');
        $result = $service->addGoods($dataArray,$wu_id);
        if (!empty($result)){
            //清空缓存
            $tag = sprintf(config('cache.card_extend'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     * 获取新华扬CRM用户拓展信息
     * @return \think\response\Json
     */
    public function getExtendInfo(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getExtendInfo($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取语音素材
     * @param $corpid
     * @param $media_id
     * @return \think\response\Json
     * @throws \Exception
     */
    public function getMaterial($corpid,$media_id){
        $wu_id = Token::getCurrentUid();
        $contact = new WorkAI();
        $result = $contact->getMaterial($corpid,$media_id,$wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     * 新华扬CRM添加拓展信息
     * @return \think\response\Json
     */
    public function addExtendInfo(){
        (new CardExtendParameter())->goCheck();
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $dataArray = input('post.');
        $result = $service->addExtendInfo($dataArray,$wu_id);
        if (!empty($result)){
            //清空缓存
            $tag = sprintf(config('cache.card_extend'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     * 新华扬CRM名片视频信息获取
     * @return \think\response\Json
     */
    public function getVideoInfo(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getVideoInfo($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 新华扬CRM获取分享语
     * @return \think\response\Json
     */
    public function getShareWord(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getShareWord($wu_id);
        if (!empty($result)){

            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 新华扬CRM更新分享语
     * @return \think\response\Json
     */
    public function updateShareWord(){
        (new CardBaseParameter())->goCheck();
        $wu_id = Token::getCurrentUid();
        $dataArray = input("post.");
        $service = new EnterpriseService();
        $result = $service->updateShareWord($dataArray,$wu_id);
        if (!empty($result)){
            //清空缓存
            $tag = sprintf(config('cache.card_extend'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 新华扬CRM获取欢迎语
     * @return \think\response\Json
     */
    public function getWelcomeWord(){
        $wu_id = Token::getCurrentUid();
        $service = new EnterpriseService();
        $result =  $service->getWelcomeWord($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 新华扬CRM更新欢迎语
     * @return \think\response\Json
     */
    public function updateWelcomeWord(){
        $wu_id = Token::getCurrentUid();
        $dataArray = input("post.");
        $service = new EnterpriseService();
        $result = $service->updateWelcomeWord($dataArray,$wu_id);
        if (!empty($result)){
            //清空缓存
            $tag = sprintf(config('cache.card_extend'),$wu_id);//缓存key
            Redis::getInstance()->del(1,$tag);//删除缓存
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 新华扬CRM获取用户基本信息
     * @return string
     */
    public function getUserInfoByUid($uid){
        $wu_id = Token::getCurrentUid();
        $service = new UserService();
        $result =  $service->getUserInfo($wu_id,$uid);
        $result->visible(['user']);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 请求成交率数量
     * @return string
     */
    public function getRateNum(){
        $wu_id = Token::getCurrentUid();
        $type = input('post.');
        $service = new UserService();
        $result = $service->getRateNum($wu_id,$type);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 新华扬CRM获取客户列表
     * @return string
     */
    public function getUserList($type){
        $wu_id = Token::getCurrentUid();
        $service = new UserService();
        $result = $service->getUsetList($wu_id,$type);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取用户信息
     * @param $id
     * @return string
     */
    public function getUserById($id){
        $wu_id = Token::getCurrentUid();
        $service = new UserService();
        $user = $service->getUserInfo($wu_id,$id);
        if (!empty($user)){
            $user->hidden(['openid','session_key','unionid','web_rate','user_rate','product_rate']);
            $data['code'] = 200;
            $data['content'] = $user;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 添加用户标签
     * @return string
     */
    public function addLabelByUser(){
        (new LabelParameter())->goCheck();
        $uid  = Token::getCurrentUid();
        $data = input('post.');
        $param = ['wu_id'=>$uid,'user_id'=>$data['user_id'],'weight'=>3,'data'=>$data];
        //行为监听
        Hook::exec('app\api\behavior\FollowBehavior','actionBegin',$param);
        $service = new UserService();
        $result =  $service->addLabelByUser($data,$uid);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取标签列表
     * @return string
     */
    public function getLabelList(){
        $uid  = Token::getCurrentUid();
        $service = new UserService();
        $result =  $service->getLabelList($uid);
        $result =  $result->toArray();
        foreach ($result as $key=>$value){
            $result[$key]['users'] = count($result[$key]['users']);
        }
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取标签用户
     * @param $id
     * @return string
     */
    public function getUserByLabel($id){
        $uid  = Token::getCurrentUid();
        $service = new UserService();
        $result = $service->getUserByLabel($id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }
    /**
     * 获取用户标签
     * @param $id
     * @return string
     */
    public function getLabelByUser($uid){
        $wu_id  = Token::getCurrentUid();
        $service = new UserService();
        $result = $service->getLabelBbyUser($uid,$wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 删除标签
     * @param $id
     * @return string
     */
    public function deleteLabel($id){
        $wu_id = Token::getCurrentUid();
        $service = new UserService();
        $result = $service->deleteLabel($id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 更新标签信息
     * @return string
     */
    public function updateLabel(){
        $uid = Token::getCurrentUid();
        $data = input("post.");
        $service = new UserService();
        $result = $service->updateLabel($data['data'],$data['users'],$data['id']);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 添加标签
     * @return string
     */
    public function addLabel(){
        $uid = Token::getCurrentUid();
        $data = input('post.');
        $service = new UserService();
        $result = $service->addLabel($uid,$data['data'],$data['users']);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 新华扬CRM更新用户信息
     * @param $id
     * @return string
     */
    public function updateUser($user_id){
        $data = input('post.');
        (new UserParameter())->goCheck();
        $wu_id = Token::getCurrentUid();
        if (isset($data['close_time'])){
            $weight = 1;
        }elseif (isset($data['close_status'])){
            $weight = 2;
        }else{
            $weight = 5;
        }
        $param = ['wu_id'=>$wu_id,'user_id'=>$user_id,'weight'=>$weight,'data'=>$data];
        //行为监听
        Hook::exec('app\api\behavior\FollowBehavior','actionBegin',$param);
        $service = new UserService();
        $result = $service->updateUser($data,$wu_id,$user_id);
        if ($result){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取跟进数据
     * @param $id
     * @param $time
     * @return string
     */
    public function getFollowData($id,$time){
        $uid = Token::getCurrentUid();
        $service = new UserService();
        $result =  $service->getFollowData($id,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 新华扬CRM跟进列表
     * @param $uid
     * @param $page
     * @return string
     */
    public function getFollowList($uid,$page){
        $wu_id = Token::getCurrentUid();
        $service = new UserService();
        $result =  $service->getFollowList($uid,$wu_id,$size = 5,$page);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 新华扬人事所有统计信息
     * @param $time
     * @param $type
     * @return string
     */
    public function getAllData($time,$type){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new \app\api\service\ThirdApp();
        $result =  $service->getData($t_id,$type,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取一个管理员下的所有成交率
     * @return string
     */
    public function getAllRateNum(){
        $t_id = Token::getCurrentTokenVar('t_id');
        $type = input('post.');
        $service = new UserService();
        $result =  $service->getAllRateNum($t_id,$type);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取总跟进数
     * @param $time
     * @return string
     */
    public function getAllFollowData($time){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new UserService();
        $result =  $service->getAllFollowData($t_id,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取总跟进数
     * @param $time
     * @return string
     */
    public function getUserNum($time){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new UserService();
        $result =  $service->getUserNum($t_id,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 用户排序
     * @return string
     */
    public function getUserByCondition($type,$num){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new UserService();
        $result =  $service->getUserByCondition($t_id,$type,$num);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取用户成交率信息
     * @param $begin
     * @param $end
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getUserRateNum($begin,$end){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new UserService();
        $result = $service->getUserRateNum($t_id,$begin,$end);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
       return json_encode($data);
    }


    /**
     * 获取用户各项参数
     * @return string
     */
    public function getUserData($uid){
        $wu_id =  Token::getCurrentUid();
        $service = new UserService();
        $result =  $service->getUserData($uid);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }


}